package ch.hoth.java.joocal.ui;

import java.util.Date;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.custom.SashForm;

import com.google.gdata.data.DateTime;
import com.google.gdata.data.extensions.EventEntry;

import ch.hoth.java.joocal.Calendar;
import ch.hoth.java.joocal.DateUtil;
import ch.hoth.java.joocal.Event2;

public class CalendarWidget extends CTabItem implements Observer{

	private Calendar calendar;
	private ListViewer eventsViewer;
	
	public CalendarWidget(CTabFolder parent, Calendar calendar){
		super(parent, SWT.CLOSE);
		this.calendar = calendar;
		initialize();
	}
	
	public Calendar getCalendar(){
		return calendar;
	}
	
	private void initialize(){
		setText(calendar.getCalendarName());
		
		SashForm content = new SashForm(getParent(), SWT.NONE);
		eventsViewer = new ListViewer(content, SWT.MULTI);
		eventsViewer.setContentProvider(new CalendarContentProvider());
		eventsViewer.setLabelProvider(new CalendarLabelProvider());
		eventsViewer.setInput(calendar.getEvents());
		this.setControl(content);
		
		eventsViewer.addSelectionChangedListener(new ISelectionChangedListener(){
			public void selectionChanged(SelectionChangedEvent arg0) {
				ISelection select = eventsViewer.getSelection();
				if (select instanceof IStructuredSelection){
					Event2 event = (Event2) ((IStructuredSelection) select).getFirstElement();
					if (!event.isPublished()){
						StatusLineManager.setStatus("This event is not yet published");
					}
					else{
						StatusLineManager.setStatus("This event is already available online");
					}
				}
			}
		});
	}
	
	public void update(Observable o, Object arg) {
		if (arg instanceof Date){
			eventsViewer.setInput(null);
			eventsViewer.setInput(calendar.getEvents((Date) arg));	
			eventsViewer.refresh();
			StatusLineManager.setStatus(DateUtil.format("E d MMM y", (Date) arg));
		}
		
	}
	
	
	private class CalendarContentProvider implements IStructuredContentProvider{

		public void dispose() {
			// TODO Auto-generated method stub
			
		}

		public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
			// TODO Auto-generated method stub
			
		}

		public Object[] getElements(Object arg0) {
			return ((List<Event2>) arg0).toArray();
		}
		
	}
	
	private class CalendarLabelProvider extends LabelProvider{

		@Override
		public String getText(Object arg0) {
			Event2 event = (Event2) arg0;
			EventEntry e = event.getEvent();
			StringBuilder builder = new StringBuilder();
			DateTime starttime = e.getTimes().get(0).getStartTime();
			DateTime endtime = e.getTimes().get(0).getEndTime();
			
			Date d = new Date(starttime.getValue());
			
			String staDate = DateUtil.format("H:m", starttime);
			String stoDate = DateUtil.format("H:m", endtime);
			if (!event.isPublished()){
				builder.append("* ");
			}
			builder.append(staDate);
			builder.append(" - " + stoDate);
			builder.append(" " + e.getTitle().getPlainText());
			return builder.toString();
		}
		
	}
	
}
